<?php

namespace app\common\libs\helper;

/**
 * 文件助手类
 *
 * @Author tanruizheng
 * @DateTime 2021-07-30
 */
class FileHelper
{
    /**
     * 删除目录
     *
     * @Author tanruizheng
     * @DateTime 2021-07-23
     * @param [type] $dir
     * @return void
     */
    public function deleteDir($path){
        if(is_dir($path)){
            $scandir = scandir($path);
            foreach ($scandir as $key => $value) {
                if($value != '.' && $value != '..'){
                    if(is_dir($path .'/'. $value)){
                        $this->deleteDir($path . '/'. $value);
                        rmdir($path .'/'. $value);
                    }else{
                        unlink($path .'/'. $value);
                    }
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 复制目录
     *
     * @Author tanruizheng
     * @DateTime 2021-08-02
     * @param [type] $fromPath
     * @param [type] $toPath
     * @return void
     */
    public function copyDir($fromPath,$toPath){
        if(is_dir($fromPath)){
            if(!is_dir($toPath)){
                mkdir($toPath,0777,true);
            }
            $scandir = scandir($fromPath);
            foreach ($scandir as $key => $value) {
                if($value != '.' && $value != '..'){
                    if(is_dir($fromPath . '/' . $value)){
                        $this->coopyDir($fromPath . '/'. $value, $toPath . '/' . $value);
                    }else{
                        copy($fromPath . '/' . $value,$toPath . '/' . $value);
                    }
                }
            }
            return false;
        }
        return false;
    }
}